package com.jacen.fzxs.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jacen.fzxs.entity.UserEntity;
import com.jacen.fzxs.service.IUserService;
import com.jacen.fzxs.util.DateUtils;
import com.jacen.fzxs.util.EntityUtils;
import com.jacen.fzxs.util.MD5;
import com.jacen.fzxs.util.ServletUtils;
import com.jacen.fzxs.util.Utils;

/**
 * 用户Controller
 * @author Administrator
 *
 */
@Controller
public class UserController extends BaseController {
	
	@Resource
	private IUserService userService;
	
	private static Logger logger = LoggerFactory.getLogger(UserController.class);
	
	/**
	 * 获取用户列表
	 * @param request
	 * @param curPage
	 * @param modelMap
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/user/list.do")
	public String getPage(HttpServletRequest request,Integer curPage,ModelMap modelMap) throws Exception{
		Map<String, String> params = ServletUtils.getParameterMap(request);
		List<UserEntity> list = userService.getPage(getPager(curPage, request), params);
		
		modelMap.addAttribute("list", list);
		modelMap.addAttribute("userName", params.get("userName"));
		logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss")+"用户："+getLocalUser(request).getUserName()+"查看用户列表");
		return "user/list";
	}
	
	/**
	 * 进入新增页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/user/insert.do", method = RequestMethod.GET)
	public String enterInsert(HttpServletRequest request,HttpServletResponse response){
		logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss")+"用户："+getLocalUser(request).getUserName()+"进入新增用户界面");
		return "user/insert";
	}
	
	/**
	 * 新增
	 * @param userEntity
	 * @param request
	 * @param response
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/user/insert.do" , method = RequestMethod.POST)
	@ResponseBody
	public String postInsert(UserEntity userEntity,HttpServletRequest request,HttpServletResponse response) throws Exception{
		boolean flag = userService.existByKey("userName", userEntity.getUserName());
		if(flag) return Utils.errorJsonResp("该用户已存在，请重新输入");
		userEntity.setId(EntityUtils.createId32());
		String pwd = MD5.genPassword(6);
		userEntity.setPassword(MD5.MD5Encode(pwd));
		userService.insert(userEntity);
		logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss")+"用户："+getLocalUser(request).getUserName()+"注册新用户："+userEntity.getUserName());
		return Utils.normalJsonResp(pwd);
	}
	/**
	 * 进入修改页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/user/update.do", method = RequestMethod.GET)
	public String enterUpdate(HttpServletRequest request,String id,ModelMap modelMap){
		logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss")+"用户："+getLocalUser(request).getUserName()+"进入修改用户界面");
		UserEntity userEntity = userService.getById(id);
		modelMap.put("entity", userEntity);
		return "user/update";
	}
	/**
	 * 删除
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("/user/delete.do")
	@ResponseBody
	public String delete(HttpServletRequest request,String id){
		userService.deleteById(id);
		logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss")+"用户："+getLocalUser(request).getUserName()+"删除用户,id"+id);
		return Utils.normalJsonResp();
	}
	
	/**
	 * 查看
	 * @param request
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/user/view.do")
	public String view(HttpServletRequest request,String id,ModelMap modelMap){
		UserEntity userEntity = userService.getById(id);
		modelMap.addAttribute("user", userEntity);
		logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss")+"用户："+getLocalUser(request).getUserName()+"查看用户信息,id"+id);
		return "user/view";
	}
	
	/**
	 * 批量删除
	 * @param request
	 * @param ids
	 * @return
	 */
	@RequestMapping("/user/batchDelete.do")
	public String batchDelete(HttpServletRequest request,String[] ids){
		for(String id : ids){
			userService.deleteById(id);
		}
		logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss")+"用户："+getLocalUser(request).getUserName()+"批量删除用户数："+ids.length);
		return "redirect:/user/list.do";
	}
	
	/**
	 * 重置密码
	 * @param request
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/user/resetPwd.do", method = RequestMethod.POST)
	@ResponseBody
	public String resetPwd(HttpServletRequest request,String id) throws Exception{
		UserEntity userEntity = userService.getById(id);
		userEntity.setPassword(MD5.MD5Encode("888888"));
		userService.update(userEntity);
		logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss") +"->用户"+userEntity.getUserName()+"密码重置成功");
		return Utils.normalJsonResp("密码重置成功，密码为：888888");
	}

}
